package com.shop2cn.iapi.wxcomponent.testcase;

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.shop2cn.iapi.wxcomponent.parameter.ConfirmAuthorizationBean;
import com.shop2cn.iapi.wxcomponent.service.ConfirmAuthorizationCall;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.business.service.WxComponentService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.Mchapp;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*商户确认授权
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ConfirmAuthorization{
	private static ConfirmAuthorizationBean confirmauthorizationBean;
	private static ConfirmAuthorizationCall confirmauthorizationCall;
	DefGodMchWapper DefGodMchWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商户确认授权");
	}

	@Before
	public void caseUp() {
		confirmauthorizationBean=new ConfirmAuthorizationBean();
		confirmauthorizationCall=new ConfirmAuthorizationCall();
		DefGodMchWapper=new DefGodMchWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(confirmauthorizationCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private Map getDefHeader() {
		Map m=new HashMap();
//		mock:mock-enable
		m.put("mock", "mock-enable");
//		mockId-1:wxcomponent-getAuthInfo-11
		m.put("mockId-1", TestDataManager.getRandomUUID().toString());
//		mockApi-1:wxcomponent-getAuthInfo-Normal
		m.put("mockApi-1", "wxcomponent-getAuthInfo-Normal");
//		mockId-2:wxcomponent-getAppInfo-11
		m.put("mockId-2", TestDataManager.getRandomUUID().toString());
//		mockApi-2:wxcomponent-getAppInfo-Normal
		m.put("mockApi-2", "wxcomponent-getAppInfo-Normal");
		return m;
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConfirmAuthorization_001() {
		Logger.start(true,"商户确认授权");
		try {
			//TODO 需要加mock 插入记录mchapp
			confirmauthorizationBean.setMchId(10000);
			confirmauthorizationBean.setAuthorizationCode("1234");
			confirmauthorizationCall.setData(confirmauthorizationBean);
			Map m=getDefHeader();
			String atoken=TestDataManager.getRandomNum(10);
			String appid=TestDataManager.getRandomNum(10);
			String rtoken=TestDataManager.getRandomNum(10);
			m.put("mockParam-1-authorizerAccessToken",atoken);
			m.put("mockParam-1-authorizerAppid",appid);
			m.put("mockParam-1-authorizerRefreshToken",rtoken);
			
			String nickName=TestDataManager.getRandomNum(10);
			String headImg=TestDataManager.getRandomNum(10);
			String principalName=TestDataManager.getRandomNum(10);
//		    "nick_name":"${nickName=nick_name}",
			m.put("mockParam-2-nickName",nickName);
//		    "head_img":"${headImg=head_img}",
			m.put("mockParam-2-headImg",headImg);
//		    "principal_name":"${principalName=principal_name}",
			m.put("mockParam-2-principalName",principalName);
			PayGateWayCallService.addMock(confirmauthorizationCall, m);
			confirmauthorizationCall.callService();			
			Logger.verifyEquals("200", confirmauthorizationCall.getString("code"), "验证返回code");
			Mchapp mchapp = new DefGodMchWapper().selectMchappByExample(confirmauthorizationBean.getMchId()).get(0);
			Logger.debug(FormatUtil.beanToGSONString(mchapp));
			Logger.verifyEquals(atoken, mchapp.getAccessToken(), "验证AccessToken");
			Logger.verifyEquals(appid, mchapp.getAppId(), "验证appid");
			Logger.verifyEquals(rtoken, mchapp.getRefreshToken(), "验证RefreshToken");
			Logger.verifyEquals(nickName, mchapp.getAppName(), "验证AppName");
			Logger.verifyEquals(headImg, mchapp.getHeadImgUrl(), "验证headImgUrl");
			Logger.verifyEquals(principalName, mchapp.getPrincipalName(), "验证PrincipalName");
			DefGodMchWapper.deleteMchappByMchid(confirmauthorizationBean.getMchId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConfirmAuthorization_002() {
		Logger.start(true,"无效授权失败");
		try {
			confirmauthorizationBean.setMchId(100);
			confirmauthorizationBean.setAuthorizationCode("1234");
			confirmauthorizationCall.setData(confirmauthorizationBean);
			confirmauthorizationCall.callService();			
			Logger.verifyEquals("400", confirmauthorizationCall.getString("code"), "验证返回code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConfirmAuthorization_003() {
		Logger.start(true,"不输入商户号失败");
		try {
//			confirmauthorizationBean.setMchId("100");
			confirmauthorizationBean.setAuthorizationCode("1234");
			confirmauthorizationCall.setData(confirmauthorizationBean);
			confirmauthorizationCall.callService();			
			Logger.verifyEquals("400", confirmauthorizationCall.getString("code"), "验证返回code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConfirmAuthorization_004() {
		Logger.start(true,"不输入AuthorizationCode失败");
		try {
			confirmauthorizationBean.setMchId(100);
//			confirmauthorizationBean.setAuthorizationCode("1234");
			confirmauthorizationCall.setData(confirmauthorizationBean);
			confirmauthorizationCall.callService();			
			Logger.verifyEquals("400", confirmauthorizationCall.getString("code"), "验证返回code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConfirmAuthorization_005() {
		Logger.start(true,"商户重复确认授权");
		try {
			//TODO 需要加mock 插入记录mchapp
			confirmauthorizationBean.setMchId(10000);
			confirmauthorizationBean.setAuthorizationCode("1234");
			confirmauthorizationCall.setData(confirmauthorizationBean);
			Map m=getDefHeader();
			String atoken=TestDataManager.getRandomNum(10);
			String appid=TestDataManager.getRandomNum(10);
			String rtoken=TestDataManager.getRandomNum(10);
			m.put("mockParam-1-authorizerAccessToken",atoken);
			m.put("mockParam-1-authorizerAppid",appid);
			m.put("mockParam-1-authorizerRefreshToken",rtoken);
			
			String nickName=TestDataManager.getRandomNum(10);
			String headImg=TestDataManager.getRandomNum(10);
			String principalName=TestDataManager.getRandomNum(10);
//		    "nick_name":"${nickName=nick_name}",
			m.put("mockParam-2-nickName",nickName);
//		    "head_img":"${headImg=head_img}",
			m.put("mockParam-2-headImg",headImg);
//		    "principal_name":"${principalName=principal_name}",
			m.put("mockParam-2-principalName",principalName);
			PayGateWayCallService.addMock(confirmauthorizationCall, m);
			confirmauthorizationCall.callService();			
			Logger.verifyEquals("200", confirmauthorizationCall.getString("code"), "验证返回code");
			Mchapp mchapp = new DefGodMchWapper().selectMchappByExample(confirmauthorizationBean.getMchId()).get(0);
			Logger.debug(FormatUtil.beanToGSONString(mchapp));
			Logger.verifyEquals(atoken, mchapp.getAccessToken(), "验证AccessToken");
			Logger.verifyEquals(appid, mchapp.getAppId(), "验证appid");
			Logger.verifyEquals(rtoken, mchapp.getRefreshToken(), "验证RefreshToken");
			Logger.verifyEquals(nickName, mchapp.getAppName(), "验证AppName");
			Logger.verifyEquals(headImg, mchapp.getHeadImgUrl(), "验证headImgUrl");
			Logger.verifyEquals(principalName, mchapp.getPrincipalName(), "验证PrincipalName");
			
			confirmauthorizationCall=new ConfirmAuthorizationCall();
			m=getDefHeader();
			atoken=TestDataManager.getRandomNum(10);
			rtoken=TestDataManager.getRandomNum(10);
			m.put("mockParam-1-authorizerAccessToken",atoken);
			m.put("mockParam-1-authorizerAppid",appid);
			m.put("mockParam-1-authorizerRefreshToken",rtoken);
			
			nickName=TestDataManager.getRandomNum(10);
			headImg=TestDataManager.getRandomNum(10);
			principalName=TestDataManager.getRandomNum(10);
//		    "nick_name":"${nickName=nick_name}",
			m.put("mockParam-2-nickName",nickName);
//		    "head_img":"${headImg=head_img}",
			m.put("mockParam-2-headImg",headImg);
//		    "principal_name":"${principalName=principal_name}",
			m.put("mockParam-2-principalName",principalName);
			PayGateWayCallService.addMock(confirmauthorizationCall, m);
			confirmauthorizationCall.setData(confirmauthorizationBean);
			confirmauthorizationCall.callService();			
			Logger.verifyEquals("200", confirmauthorizationCall.getString("code"), "验证返回code");
			mchapp = new DefGodMchWapper().selectMchappByExample(confirmauthorizationBean.getMchId()).get(0);
			mchapp = new DefGodMchWapper().selectMchappByExample(confirmauthorizationBean.getMchId()).get(0);
			Logger.debug(FormatUtil.beanToGSONString(mchapp));
			Logger.verifyEquals(atoken, mchapp.getAccessToken(), "验证AccessToken");
			Logger.verifyEquals(appid, mchapp.getAppId(), "验证appid");
			Logger.verifyEquals(rtoken, mchapp.getRefreshToken(), "验证RefreshToken");
			Logger.verifyEquals(nickName, mchapp.getAppName(), "验证AppName");
			Logger.verifyEquals(headImg, mchapp.getHeadImgUrl(), "验证headImgUrl");
			Logger.verifyEquals(principalName, mchapp.getPrincipalName(), "验证PrincipalName");
			DefGodMchWapper.deleteMchappByMchid(confirmauthorizationBean.getMchId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConfirmAuthorization_006() {
		Logger.start(true,"绑定，授权，解绑，授权--成功");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
            String timestamp=String.valueOf(YMTDateUtil.getDate().getTime()/1000);
            WxComponentService.deleteMchinfo(mchid);
            //授权
            String appid = WxComponentService.ConfirmAuthorization(mchid);
            WxComponentService.AuthorizationCallBack(appid, "authorized");
            //取消授权
            WxComponentService.AuthorizationCallBack(appid, "unauthorized");
            
            boolean confirmAuthorization = WxComponentService.ConfirmAuthorization(mchid,appid);
            WxComponentService.AuthorizationCallBack(appid, "authorized");
            
            Logger.verifyEquals(confirmAuthorization, true, "判断");
            
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}